//
//  CrowdFundingAnotherViewController.m
//  ioschishan
//
//  Created by 马田成 on 2017/7/12.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "CrowdFundingAnotherViewController.h"
#import "HomeThreeTableViewCell.h"
#import "HomeCrowdfundingViewController.h"
#import "bottomTableViewCell.h"
#import "MJRefresh.h"
@interface CrowdFundingAnotherViewController ()<UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation CrowdFundingAnotherViewController
- (void)viewWillAppear:(BOOL)animated{

    [self creationData];
    
    __weak typeof(self) weakself = self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself creationDataJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself creationData];
        
        [self.tableView.mj_header beginRefreshing];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self creationView];
    [self regeditCell];

    
    // Do any additional setup after loading the view.
}
/**
 
 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
 
 将要开始编辑时的回调，返回为NO，则不能编辑
 
 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
 
 已经开始编辑时的回调
 
 - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
 
 将要结束编辑时的回调
 
 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
 
 已经结束编辑的回调
 
 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   编辑文字改变的回调
 
 - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;
 
 编辑文字改变前的回调，返回NO则不能加入新的编辑文字
 
 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
 
 搜索按钮点击的回调
 
 - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;
 
 书本按钮点击的回调
 
 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
 
 取消按钮点击的回调
 
 - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;
 
 搜索结果按钮点击的回调
 
 
 
 - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
 
 搜索栏的附加试图中切换按钮触发的回调
 
 */
- (void)creationView{

    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 40)];
    [self.view addSubview:search];
    search.placeholder = @"搜索";
    search.backgroundColor = [UIColor lightGrayColor];
    search.delegate = self;
    
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 40, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 40 - 49 - 40);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    

    [self.view addSubview:self.tableView];
    
    
}
-(NSMutableArray *)arr{

    if (_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
    

}
- (void)creationData{
    

    [self.tableView.mj_header endRefreshing];
    
    
    
    _page = 1;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:@"沈阳" forKey:@"city"];
    [productParams setValue:_orderStatus forKey:@"classify"];
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"page"];
    
    [[MNetworkHandler sharedInstance]conURL:home_CrowdCrowd networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
                
                _dataArr = [returnData objectForKey:@"data"];
                NSLog(@"!!!!!!!!!!!!!!!%@", _dataArr);
                
                [self.tableView reloadData];

            
        }
            
        
        else {
                
                //                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                
            
        }
            
        
    } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            
        
    }];
        

    
    
}

- (void)creationDataJZ{
    
    
    _page++;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:@"沈阳" forKey:@"city"];
    [productParams setValue:_orderStatus forKey:@"classify"];
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"page"];
    
    [[MNetworkHandler sharedInstance]conURL:home_CrowdCrowd networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            NSMutableArray *arr =[returnData objectForKey:@"data"];
            
            if (arr.count == 0) {
                
                [self.tableView.mj_footer endRefreshing];
                
            }else {
                
                [self.dataArr addObjectsFromArray:arr];
                NSLog(@"%@", self.dataArr);
  
                
                
            }
            
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
    
    
    
}




- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeThreeTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeThreeTableViewCell"];
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return screenScale(190);

}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [tableView registerClass:[bottomTableViewCell class] forCellReuseIdentifier:@"bottomTableViewCell"];
    bottomTableViewCell *cell = (bottomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"bottomTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *urlN = manage(AP_HOST, _dataArr[indexPath.row][@"pic"]);
    NSURL *urlW = [NSURL URLWithString:urlN];
    [cell.leftImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.nameLable.text = _dataArr[indexPath.row][@"title"];
    
    cell.textLable.text = _dataArr[indexPath.row][@"content"];
    
    [cell.progressView setProgress:[_dataArr[indexPath.row][@"money"] floatValue] / [_dataArr[indexPath.row][@"target"] floatValue] animated:YES];
    
    cell.progressLable.text = [NSString stringWithFormat:@"%.0f",[_dataArr[indexPath.row][@"money"] floatValue] / [_dataArr[indexPath.row][@"target"] floatValue]];
    
    NSString *urlN1 = manage(AP_HOST, _dataArr[indexPath.row][@"header_img"]);
    NSURL *urlW1 = [NSURL URLWithString:urlN1];
    [cell.headImg sd_setImageWithURL:urlW1 placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.personLable.text = _dataArr[indexPath.row][@"user_name"];
    
    cell.moneyLable.text = _dataArr[indexPath.row][@"target"];
    
    cell.peopleLable.text = _dataArr[indexPath.row][@"support"];
    
    cell.dayLable.text = _dataArr[indexPath.row][@"days"];

    
    
    
    
    return cell;
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    
    HomeCrowdfundingViewController *vc = [[HomeCrowdfundingViewController alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
    vc.crowd_id =_dataArr[indexPath.row][@"crowd_id"];
    
    [self.navigationController pushViewController:vc animated:YES];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
